<?php
/*
 * @author eBIZ Team <dev@jungo.com.cn>
 * @copyright  Copyright (c) , ShenZhen JunGO Technology Co., Ltd. All rights reserved.
 * @license  Commercial authorization, unauthorized use and modification are prohibited
 * @url www.jungo.com.cn
 */

namespace ckeeper\http;



use ckeeper\utils\ManagerActionRecorder;
use ckeeper\utils\PasswordEncrypt;
use ckeeper\utils\EntityUtil;
use ckeeper\utils\UuidGenerator;
use ckeeper\utils\Validate;
use Selibra\Di\Annotations\Autowired;
use Selibra\Di\Annotations\Component;
use Selibra\Http\Annotations\Controller;
use Selibra\Http\Annotations\Route;
use Selibra\Model\EntityProxy;
use ckeeper\db\Transaction;
use ckeeper\entity\ManagerEntity;
use ckeeper\services\ManagerService;


#[Component]
#[Controller("/system/manager")]
class Manager extends http
{

    /**
     * @var ManagerService
     */
    #[Autowired]
    protected ManagerService $managerService;


    /**
     * 管理员首页
     */
    #[Route("/search")]
    public function index()
    {
        $search = $this->getRequest()->getGet('search');
        $managerEntityList = $this->managerService->searchManager($search);
        $managerList = EntityUtil::eachArray($managerEntityList, function ($data) {
            unset($data['password'], $data['salt'], $data['token']);
            return $data;
        });
        return $this->success("获取成功", [
            'managerList' => $managerList
        ]);
    }


    /**
     * 添加管理员
     * @return mixed
     */
    #[Route("/","PUT")]
    public function add()
    {
        $inputData = $this->getRequest()->getPost();
        if (empty($inputData['role'])) {
            return $this->error("请给管理员选择一个角色",null,ERR_10005);
        } elseif (empty($inputData['username']) || empty($inputData['password']) || empty($inputData['name'])) {
            return $this->error("缺少必填参数，请确认",null,ERR_10005);
        } elseif (!empty($inputData['email']) && !Validate::isEmail($inputData['email'])) {
            return $this->error("邮箱格式不正确",null,ERR_10006);
        } elseif (!empty($inputData['mobile']) && !Validate::isMobile($inputData['mobile'])) {
            return $this->error("手机号码格式不正确",null,ERR_10006);
        }
        // 保存管理员信息
        $uuidGenerator = new UuidGenerator();
        $managerEntity = new ManagerEntity();
        $managerEntity->setUuid($uuidGenerator->generator());
        $managerEntity->setName($inputData['name']);
        $managerEntity->setMobile($inputData['mobile'] ?: null);
        $managerEntity->setEmail($inputData['email'] ?: null);
        $managerEntity->setUsername($inputData['username']);

        // 设置密码
        $salt = PasswordEncrypt::randomGenSalt();
        $password = PasswordEncrypt::encrypt($inputData['password'], $salt);
        $managerEntity->setPassword($password);

        Transaction::begin();
        try {
            // 创建
            $managerEntity = $this->managerService->createManager($managerEntity);

            // 记录操作日志
            ManagerActionRecorder::info(
                $managerEntity->getUuid(),
                'managerAdd',
                '新增管理员账户:' . $managerEntity->getName(),
                $this->getRequest(),
                EntityProxy::entityConversionToArray($managerEntity)
            );

            Transaction::commit();
        } catch (\Exception $exception) {
            Transaction::rollback();
            return $this->error("账户新增失败:" . $exception->getMessage());
        }
        return $this->success("账户新增成功");
    }


    /**
     * 修改管理员账户
     * @return mixed
     */
    public function update()
    {

    }

}
